package com.ky.serialport.utils;

import java.nio.ByteBuffer;

/**
 *数据转换工具
 */
public class MyFunc {

	public static int getPower(byte a, byte b) {
		return twoByte2IntNoSign(a, b);
	}

	public static int getSignal(byte a) {
		return 0x000000ff & a;
	}

	public static int speed(byte a, byte b) {
		return twoByte2IntNoSign(b,a);
	}

	//双字节转int
    //2020年3月12日16:01:39  验证正确，无符号就返回int类型
	public static int twoByte2IntNoSign(byte a, byte b) {
		return (a & 0xff)<<8 | (b & 0xff);
	}

    //双字节转int  有符号，有符号就返回short类型
    //2020年3月12日16:00:57 验证正确
    public static short twoBytes2IntSign(byte a, byte b) {
        return (short) ((a<< 8 & 0xffff) | (b & 0xff));
    }

	static	 public  int twoByte2int (byte[] b  , int  index){
		return   b[index+1] & 0xFF | (b[index] & 0xFF) << 8;
	}

	// 4字节byte转无符号int
	public static int fourByte2int(byte[] b, int index) {
		return (b[index + 3] & 0xFF) | (b[index + 2] & 0xFF) << 8 | (b[index + 1] & 0xFF) << 16 | (b[index] & 0xFF) << 24;
	}





	public static int threeBytes2Int(byte[] buffer , int  index){
		return (buffer[index] << 16 & 0xffffffff) | (buffer[index +1 ] << 8 & 0xffffffff) | (buffer[index+2] & 0xffffffff);
	}

	/**
	 * @Description ：不带符号
	 */
	public static int threeBytesToInt (byte[] bytes , int index){
		byte[] by = new byte[6];
		by[0] = bytes[index+2];
		by[1] = bytes[index+1];
		by[2] = bytes[index];
		int l;
		l = by[ 0];
		l &= 0xff;
		l |= ((long) by[ 1] << 8);
		l &= 0xffff;
		l |= ((long) by[ 2] << 16);
		l &= 0xffffff;
		return l;
	}

	/**
	 * 字节转换为浮点
	 *
	 * @param b     字节（至少4个字节）
	 * @param index 开始位置
	 * @return
	 */
	public static float byte2float(byte[] b, int index) {
		byte[] by = new byte[4];
		by[0] = b[index + 3];
		by[1] = b[index + 2];
		by[2] = b[index + 1];
		by[3] = b[index];
		int l;
		l = by[0];
		l &= 0xff;
		l |= ((long) by[1] << 8);
		l &= 0xffff;
		l |= ((long) by[2] << 16);
		l &= 0xffffff;
		l |= ((long) by[3] << 24);
		return Float.intBitsToFloat(l);
	}


	/** 
	  * 字节转换为浮点 
	  * @param b 字节（至少4个字节） 
	  * @param index 开始位置 
	  * @return 
	  */
	public static float fourbyte2float(byte[] b,int index){
		byte[] a = {b[index],b[index+1],b[index+2],b[index+3]};
		ByteBuffer buf=ByteBuffer.allocateDirect(4); //无额外内存的直接缓存
		//buf=buf.order(ByteOrder.LITTLE_ENDIAN);//默认大端，小端用这行
		buf.put(a);
		buf.rewind();
		float f2=buf.getFloat();
		return f2;
	}

	//温湿度大气压
	public static int byte4Toint(byte[] b, int index) {
		byte[] by = new byte[4];
		by[0] = b[index+3];
		by[1] = b[index+2];
		by[2]=  b[index+1];
		by[3] = b[index];
		int l;
		l = by[ 0];
		l &= 0xff;
		l |= ((long) by[ 1] << 8);
		l &= 0xffff;
		l |= ((long) by[2] << 16);
		l &= 0xffffff;
		l |= ((long) by[3] << 24);
		l &= 0xffffffff;
		return l;
	}

	//-------------------------------------------------------
	// 判断奇数或偶数，位运算，最后一位是1则为奇数，为0是偶数
    static public int isOdd(int num)
	{
		return num & 0x1;
	}
    //-------------------------------------------------------
    static public int HexToInt(String inHex)//Hex字符串转int
    {
    	return Integer.parseInt(inHex, 16);
    }
    //-------------------------------------------------------
    static public byte HexToByte(String inHex)//Hex字符串转byte
    {
    	return (byte)Integer.parseInt(inHex,16);
    }
    //-------------------------------------------------------
    static public String Byte2Hex(Byte inByte)//1字节转2个Hex字符
    {
    	return String.format("%02x", inByte).toUpperCase();
    }
    //-------------------------------------------------------
	public static String ByteArrToHex(byte[] inBytArr)//字节数组转转hex字符串
	{
		StringBuilder strBuilder=new StringBuilder();
		int j=inBytArr.length;
		for (int i = 0; i < j; i++)
		{
			strBuilder.append(Byte2Hex(inBytArr[i]));
			strBuilder.append(" ");
		}
		return strBuilder.toString(); 
	}
  //-------------------------------------------------------
    static public String ByteArrToHex(byte[] inBytArr,int offset,int byteCount)//字节数组转转hex字符串，可选长度
	{
    	StringBuilder strBuilder=new StringBuilder();
		int j=byteCount;
		for (int i = offset; i < j; i++)
		{
			strBuilder.append(Byte2Hex(inBytArr[i]));
		}
		return strBuilder.toString();
	}
	//-------------------------------------------------------
	//转hex字符串转字节数组
    static public byte[] HexToByteArr(String inHex)//hex字符串转字节数组
	{
		int hexlen = inHex.length();
		byte[] result;
		if (isOdd(hexlen)==1)
		{//奇数
			hexlen++;
			result = new byte[(hexlen/2)];
			inHex="0"+inHex;
		}else {//偶数
			result = new byte[(hexlen/2)];
		}
	    int j=0;
		for (int i = 0; i < hexlen; i+=2)
		{
			result[j]=HexToByte(inHex.substring(i,i+2));
			j++;
		}
	    return result; 
	}


	public static float ArryToFloat(byte[] Array, int Pos) {
		int accum = 0;
		accum = Array[Pos + 0] & 0xFF;
		accum |= (long) (Array[Pos + 1] & 0xFF) << 8;
		accum |= (long) (Array[Pos + 2] & 0xFF) << 16;
		accum |= (long) (Array[Pos + 3] & 0xFF) << 24;
		return Float.intBitsToFloat(accum);
	}
}